﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Capstone_project.DatabaseAccessLayer;
using Capstone_project.BussnessLogicLayer;

namespace Capstone_project
{
    public partial class frmAddTeam : Form
    {
        public frmAddTeam()
        {
            InitializeComponent();
        }
        
        DataSet ds1 = Common.ds;
        SqlCommand com1;
        SqlDataAdapter adap1;
        private void frmAddTeam_Load(object sender, EventArgs e)
        {
            SqlConnection con = Common.con;
            con.Open();
            // Đưa dữ liệu lên combobox Project
            string sql = "SELECT * FROM PROJECT";
            com1 = new SqlCommand(sql, con);
            adap1 = new SqlDataAdapter(com1);
            ds1 = new DataSet();
            adap1.Fill(ds1, "PROJECT");
            DataTable dt1 = ds1.Tables["PROJECT"];

            cmbProject.DataSource = dt1;
            cmbProject.DisplayMember = "NAME_PROJECT";
            cmbProject.ValueMember = "ID_PROJECT";
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (txtIdTeam.Text == "" || txtTeamName.Text == "" || txtDescription.Text == "")
            {
                MessageBox.Show("You must enter all information");
            }

            else
            {
                string sql = "INSERT INTO TEAM(ID_TEAM,NAME_TEAM,DESCRIPTION,ID_PROJECT)VALUES('" + txtIdTeam.Text + "','" + txtTeamName.Text + "','" + txtDescription.Text + "','" + cmbProject.SelectedValue + "')";
                Common common = new Common();
                common.cmdExecuteNonQuery(sql);
                MessageBox.Show("Team added successfully !");

                string sql1 = "SELECT ID FROM TREEVIEW WHERE ITEM = '"+ cmbProject.SelectedValue +"'";
                object ketqua = common.cmdExecuteScalar(sql1);
                string ketqua1 = ketqua+"";
                int ID = int.Parse(ketqua1);
                int ID1 = Int32.Parse(ketqua1);

                
                string sql2 = "INSERT INTO TREEVIEW(ITEM, PARENT_ID) VALUES('" + txtTeamName.Text + "','" + ID + "')";
                common.cmdExecuteNonQuery(sql2);
                //MessageBox.Show("Team added into Treeview table !");

            }
        }
    }
}
